package com.qianbao.qianbaobusiness.ui.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.event.EInvoiceListEvent;
import com.baseandroid.mvp.BaseMvpActivity;
import com.baseandroid.utils.MoneyUtil;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.EInvoiceListBean;
import com.qianbao.qianbaobusiness.json.EInvoiceListHeadBean;
import com.qianbao.qianbaobusiness.ui.login.EInvoiceListRvAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EInvoiceListActivity extends BaseMvpActivity<EInvoiceListView, EInvoiceListPresenter> implements EInvoiceListView, SwipeRefreshLayout.OnRefreshListener, BaseRecycleViewAdapter.OnLoadMoreListener {

    @BindView(R.id.tv_title)
    TextView           mTvTitle;
    @BindView(R.id.tv_money)
    TextView           mTvMoney;
    @BindView(R.id.tv_counts)
    TextView           mTvCounts;
    @BindView(R.id.ll_empty)
    LinearLayout       mLlEmpty;
    @BindView(R.id.rv)
    RecyclerView       mRv;
    @BindView(R.id.srl)
    SwipeRefreshLayout mSrl;
    private String mPos = "";
    private EInvoiceListRvAdapter mRvAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_einvoice_list;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        EventBus.getDefault().register(this);
        mTvTitle.setText("电子发票管理");
        mSrl.setColorSchemeColors(ContextCompat.getColor(getApplicationContext(), R.color.text_red));
        mSrl.setOnRefreshListener(this);
        mRv.setLayoutManager(new LinearLayoutManager(this));
        if (mRvAdapter == null) {
            mRvAdapter = new EInvoiceListRvAdapter(this);
        }
        mRv.setAdapter(mRvAdapter);
        mRvAdapter.setOnLoadMoreListener(this, mRv);
        mRvAdapter.setItemClickListener(new EInvoiceListRvAdapter.ClickListener() {
            @Override
            public void onItemClick(EInvoiceListBean.ItemsBean bean) {
                Bundle b = new Bundle();
                b.putString("invoiceId", bean.getInvoiceId());
                Intent intent = new Intent(EInvoiceListActivity.this, EInvoiceDetailActivity.class);
                intent.putExtra("bundle", b);
                startActivity(intent);
            }
        });
        initData();
    }

    private void initData() {
        getPresenter().getInvoiceCount();
        getPresenter().getInvoiceList(mPos);
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick(R.id.iv_back_toolbar)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void getInvoiceCountSuccess(EInvoiceListHeadBean bean) {
        mTvMoney.setText(MoneyUtil.parseMoneyWithComma(bean.getTotalMoney()));
        mTvCounts.setText(MoneyUtil.parseMoneyWithComma(bean.getCountInvoice()));
    }

    @Override
    public void getInvoiceListSuccess(EInvoiceListBean bean) {
        if (mSrl.isRefreshing()) {
            mSrl.setRefreshing(false);
        }
        if (!mRvAdapter.isLoadMoreEnable()) {
            mRvAdapter.setEnableLoadMore(true);
        }
        List<EInvoiceListBean.ItemsBean> list = bean.getItems();
        if (TextUtils.isEmpty(mPos)) {
            if (list.size() == 0) {
                mLlEmpty.setVisibility(View.VISIBLE);
            } else {
                mLlEmpty.setVisibility(View.GONE);
            }
            mRvAdapter.resetData(list);
        } else {
            if (list.size() == 0) {
                mRvAdapter.loadMoreEnd(false);
            } else if (list.size() < 20) {
                mRvAdapter.loadMoreEnd(false);
            } else {
                mRvAdapter.loadMoreComplete();
            }
            mRvAdapter.addData(list);
        }
        mPos = bean.getPos();
    }

    @Override
    public void onNetError(Throwable e) {
        if (mSrl.isRefreshing()) {
            mSrl.setRefreshing(false);
        }
    }

    @Override
    public void onRefresh() {
        mPos = "";
        initData();
    }

    @Override
    public void onLoadMore() {
        netListData();
    }

    private void netListData() {
        getPresenter().getInvoiceList(mPos);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EInvoiceListEvent event) {
        if (!TextUtils.isEmpty(event.getInvoiceId())) {
            if (mRvAdapter != null) {
                mRvAdapter.resetItem(event.getInvoiceId());
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
